package ${packageName};

import com.it.cloud.core.dao.BaseDao;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.List;

/**
* @author lt
*/
@Service
@AllArgsConstructor
public class ${name}ServiceImpl implements ${name}Service{
	private BaseDao baseDao;

	public void setSearchStr(${name} obj){
		obj.setSearchStr("");
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void add(${name} obj) {
		setSearchStr(obj);
		baseDao.save(obj);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void del(@RequestParam("ids") List<Long> idList) {
		baseDao.delByIds(${name}.class, idList);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void update(${name} obj) {
		${name} oldObj = baseDao.findById(${name}.class, obj.getId());
		<#list editProNames as proName>
		oldObj.set${proName?cap_first}(obj.get${proName?cap_first}());
		</#list>
		setSearchStr(oldObj);
		baseDao.update(oldObj);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void batchUpdate(
		List<Long> idList,
		<#list batchProNames as proName>
		Boolean ${proName}Ck,
		</#list>
		${name} obj
	) {
		List<${name}> updateList = new ArrayList<>();
		for(Long id:idList) {
			${name} oldObj = baseDao.findById(${name}.class, id);
			<#list batchProNames as proName>
			if(${proName}Ck){
				oldObj.set${proName?cap_first}(obj.get${proName?cap_first}());
			}
			</#list>
			setSearchStr(oldObj);
			updateList.add(oldObj);
		}
		baseDao.batchUpdate(updateList);
	}
}
